// utils/wechat-sdk.js
import wx from 'weixin-js-sdk';

export default {
  init(config) {
    return new Promise((resolve, reject) => {
      wx.config({
        debug: false,
        appId: config.appId,
        timestamp: config.timeStamp || "",
        nonceStr: config.nonceStr || "",
        signature: config.paySign || "",
        jsApiList: [
          'chooseWXPay',         // 微信支付
          'updateAppMessageShareData', // 分享到朋友圈
          'updateTimelineShareData',    // 分享给朋友
		  'getLocation', // 获取位置
		  // 其他需要调用的API
        ]
      });
	let timer = setTimeout(()=>{
		wx.ready(() => {
		  resolve(wx);
		});
	}, 2000)

      wx.error(err => {
        reject(err);
      });
    });
  },

  // 微信支付
  pay(options) {
	  console.log('options==========================',options)
    return new Promise((resolve, reject) => {
      wx.chooseWXPay({
        timestamp: options.timeStamp,
        nonceStr: options.nonceStr,
        package: options.package,
        signType: options.signType,
        paySign: options.paySign,
        success: resolve,
        fail: reject
      });
    });
  },
	getLocationFun(){
		return new Promise((resolve, reject)=>{
			wx.getLocation({
				type: 'wgs84',
				success (res) {
					console.log(res)
					const latitude = res.latitude
					const longitude = res.longitude
					const speed = res.speed
					const accuracy = res.accuracy
					resolve(res)
				}
			})
		})
	}
};